#include "registerface.h"
#include "ui_registerface.h"

registerface::registerface(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::registerface)
{
    ui->setupUi(this);

    setWindowIcon(QIcon(":/img/icon.jpg"));

    setWindowTitle("天天考试系统");

    m_dir = QCoreApplication::applicationDirPath();
}

/* 注册界面关闭事件 */
void registerface::closeEvent(QCloseEvent *event)
{
    ui->le_uesr->clear();
    ui->le_password->clear();
    emit close();
}

/* 点击确定按钮 */
void registerface::on_sureBtn_clicked()
{
    if (ui->le_uesr->text().size() < 2 || ui->le_password->text().size() < 2)
    {
        QMessageBox::warning(this, "注册失败", "账号或密码数量太少");
        return;
    }

    QFile stufile(m_dir + "/stuInfo.txt");
    if (stufile.exists() == false)
    {
        qDebug() << "创建学生文件" << endl;
    }

    stufile.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (stufile.atEnd() == false) //文件没结束
    {
        QString line = stufile.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    stufile.close();

    QString newline = ui->le_uesr->text() + "+" + ui->le_password->text() + "\n";
    lines.append(newline);

    stufile.open(QIODevice::WriteOnly);

    QTextStream out(&stufile);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    stufile.close();

    QMessageBox::warning(this, "注册成功", "账号已注册");
    ui->le_uesr->clear();
    ui->le_password->clear();
    emit close();
}

/* 点击取消按钮 */
void registerface::on_cancelBtn_clicked()
{
    ui->le_uesr->clear();
    ui->le_password->clear();
    emit close();
}

registerface::~registerface()
{
    delete ui;
}
